package com.bbh.plugin.single.privilege.core.service;


import com.bbh.common.domain.Menu;
import com.bbh.common.domain.MenuResult;
import com.bbh.plugin.single.privilege.core.entity.Account;
import com.bbh.plugin.single.privilege.core.entity.Resource;
import com.bbh.plugin.single.privilege.core.enums.BaseEnum;
import com.bbh.plugin.single.privilege.core.repository.ResourceMapper;
import com.bbh.plugin.single.privilege.core.vo.tree.ResourceVo;
import com.bbh.plugin.single.privilege.core.vo.tree.ZTreeNoneAjaxVo;
import com.bbh.common.constants.Constants;
import com.bbh.common.domain.Condition;
import com.bbh.common.utils.ConvertUtils;
import org.apache.commons.collections.CollectionUtils;
import org.apache.commons.collections.map.HashedMap;
import org.apache.commons.lang3.StringUtils;
import org.apache.commons.lang3.math.NumberUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.util.Assert;
import tk.mybatis.mapper.entity.Example;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import com.bbh.common.base.service.BaseService;

/**
 * Created by roman_000 on 2015/7/15.
 */
@Service
@Transactional
public class ResourceService extends BaseService<Resource, String> implements Constants {

    @Autowired
    private ResourceMapper resourceMapper;
    @Autowired
    private AccountService accountService;


    /**
     * 通过角色Id查询资源列表
     */
    public List<Resource> findResourceListByRoleId(Map<String, Object> params) {
        return resourceMapper.findResourceListByRoleId(params);
    }


    /**
     * 通过登录用户查询菜单
     *
     * @return
     */
    public MenuResult findMenuListByLoginName(String loginName, String appKey) {
        Assert.notNull(loginName, "登录名不能为空");
        Assert.notNull(appKey, "appKey不能为空");
        Account accountParams = new Account();
        accountParams.setLoginName(loginName);
        accountParams.setIsEnabled(true);
        accountParams.setDataStatus(BaseEnum.DATA_STATUS.NORMAL.getKey());
        Account account = accountService.selectOne(accountParams);
        Assert.notNull(account);
        Condition condition = new Condition();
        condition.put("loginName", loginName);
        condition.put("appKey", appKey);
        List<Menu<Menu>> menuList = resourceMapper.findMenuListByLoginName(condition);
        return buildMenuList(menuList);
    }


    /**
     * 自定义查询
     *
     * @param params
     * @return
     */
    public List<ResourceVo> findChildren(Map<String, Object> params) {
        return resourceMapper.findChildren(params);
    }

    /**
     * 删除以及子节点菜单
     *
     * @param id
     */
    public void deleteResource(String id) {
        Condition condition = new Condition();
        condition.put("parentId", id);
        List<ResourceVo> resourceVoList = resourceMapper.findChildren(condition);
        if (null != resourceVoList && resourceVoList.size() > 0) {
            for (ResourceVo resourceVo : resourceVoList) {
                if ("true".equals(resourceVo.getIsLeaf())) {
                    deleteResource(resourceVo.getId());
                    resourceMapper.deleteByPrimaryKey(resourceVo.getId());
                } else {
                    resourceMapper.deleteByPrimaryKey(resourceVo.getId());
                }
            }
        }
    }


    /*
    * ztree查询
    *
    * */
    public List<ZTreeNoneAjaxVo> selectTreeResource(Map<String, Object> params) {
        List<ZTreeNoneAjaxVo> list = resourceMapper.selectTreeResource(params);
        return list;
    }

    /**
     * java最经典的构造树形的方法
     * 用于构造菜单树形数据
     *
     * @param menuList
     * @return
     */
    private MenuResult buildMenuList(List<Menu<Menu>> menuList) {
        List<Menu<Menu>> menuRootList = new ArrayList<>();


        Map<String, Object> appMenus = new HashMap<>();

        Map<String, Menu<Menu>> menuMap = new HashMap<>();
        if (CollectionUtils.isNotEmpty(menuList)) {
            for (Menu<Menu> vo : menuList) {
                menuMap.put(vo.getId(), vo);

                if (StringUtils.isNotBlank(vo.getUrl())) {
                    appMenus.put(vo.getAppKey(), vo.getUrl());
                }
            }
            for (Menu<Menu> vo : menuList) {
                if (String.valueOf(BOSS_NODE_ID).equals(vo.getParentId())) {//顶级
                    menuRootList.add(vo);
                } else {
                    Menu<Menu> node = menuMap.get(vo.getParentId());
                    if (node != null) {
                        node.getChildren().add(vo);
                    }
                }
            }
        }
        MenuResult map = new MenuResult();
        map.setMenuList(menuRootList);
        map.setAppRootMenus(appMenus);
        return map;
    }
}
